﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Security.Permissions;
using System.Threading;
using WeatherServiceLibrary.Code;


namespace WeatherServiceLibrary
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class Service1 : IService1
    {
        //[PrincipalPermission(SecurityAction.Demand,Role="admin")]


        // tagastab viimase 24 tunni mõõtmiste tulemused. List koosneb 24 Observation elemendist. 
        //Igas Observation elemendis on iga jaama kohta üks StationObservation.
        public List<Observation> GetLast24Observations()
        {

            using (DBA.WeatherDB.WeatherBaseDataContext db = new DBA.WeatherDB.WeatherBaseDataContext())
            {

                var time = db.StationObservations.GroupBy(x => x.Timestamp).OrderByDescending(x => x.Key);
                var result = from x in time select new Observation(x.Key);
                List<Observation> obsList = result.ToList();
                return obsList;

            }
        }

        // Annab antud Stationi ID viimased etteantud arvu sissekandeid (vaikimisi 24)
        public List<StationObservation> GetStationObservations(int stationID, int count = 24)
        {
            using (DBA.WeatherDB.WeatherBaseDataContext db = new DBA.WeatherDB.WeatherBaseDataContext())
            {

                var stationObservations = db.StationObservations.Where(x => stationID == x.StationID)
                    .OrderByDescending(x => x.Timestamp).Take(count).ToList();
                var query = from x in stationObservations select new StationObservation(x);
                return query.ToList();

            }
        }

        // Tagastab hetkel kõige soojema ilmaga Stationi
        public Station GetWarmest(out double? temp)
        {
            using (DBA.WeatherDB.WeatherBaseDataContext db = new DBA.WeatherDB.WeatherBaseDataContext())
            {
                var stationObservation = db.StationObservations.OrderByDescending(x => x.AirTemperature).FirstOrDefault();
                if (stationObservation == null)
                {
                    temp = null;
                    return null;
                }
                var warmestStation = stationObservation.Station;
                temp = stationObservation.AirTemperature;
                return new Station(warmestStation);

            }
        }

        // Tagastab kõik Stationid (see tähendab, et sisuliselt saab kätte kogu informatsiooni, mida algsest XML-ist lugeda saab)
        public List<Station> GetStations()
        {

            using (DBA.WeatherDB.WeatherBaseDataContext db = new DBA.WeatherDB.WeatherBaseDataContext())
            {

                var query = from x in db.Stations select new Station(x);
                return query.ToList();

            }
        }

        // Tagastab Stationid, kus on viimase mõõtmise järgi antud temperatuurist madalam temperatuur
        public List<Station> GetStationsWhereLower(double temp)
        {
            using (DBA.WeatherDB.WeatherBaseDataContext db = new DBA.WeatherDB.WeatherBaseDataContext())
            {
                var newestObservations = db.Stations.Select(x => x.StationObservations.OrderByDescending(y => y.Timestamp).FirstOrDefault());
                //var newestObservations = db.StationObservations.OrderByDescending(y => y.Timestamp).FirstOrDefault();
                var stations = newestObservations.Where(x => x.AirTemperature < temp && x.AirTemperature != null).Select(x => new Station(x.Station)).ToList();
                return stations;

            }
        }

        // Tagastab Stationid, kus on hetkel antud temperatuurist kõrgem temperatuur
        public List<Station> GetStationsWhereHigherOrEqual(double temp)
        {
            using (DBA.WeatherDB.WeatherBaseDataContext db = new DBA.WeatherDB.WeatherBaseDataContext())
            {

                var newestObservations = db.Stations.Select(x => x.StationObservations.OrderByDescending(y => y.Timestamp).FirstOrDefault());
                var stations = newestObservations.Where(x => x.AirTemperature >= temp && x.AirTemperature != null).Select(x => new Station(x.Station)).ToList();
                return stations;

            }
        }

        // Tagastab arvu, kui suur on sademete hulk antud algus kuupäevast (k.a.) antud lõpukuupäevani (k.a) antud jaamas
        public double GetPercipitationsSum(DateTime begin, DateTime end, String stationName)
        {
            using (DBA.WeatherDB.WeatherBaseDataContext db = new DBA.WeatherDB.WeatherBaseDataContext())
            {
                var station = db.Stations.FirstOrDefault(x => x.name == stationName);
                var stationObservations = db.StationObservations.Where(x => x.Timestamp >= begin && x.Timestamp <= end);
                var precipSum = stationObservations.Where(x => station == x.Station)
                    .Sum(y => y.Precipitations) ?? 0;


                return precipSum;

            }
        }


    }
}
